﻿using JX.Core;
using JX.Core.Entity;
using JX.Infrastructure.Common;
using JX.Infrastructure.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace JX.Application
{
	/// <summary>
	/// 数据库表：Contacter 的应用层服务接口实现类.
	/// </summary>
	public partial class ContacterServiceAppDapper : ServiceAppDapper<ContacterEntity>,IContacterServiceAppDapper
	{
		#region 添加
		/// <summary>
		/// 添加数据。
		/// 1、检查要添加的数据中，联系人类型（UserType）如果为主联系人（企业），则修改数据库中的UserType为第二联系人（企业）。
		/// 2、添加数据。
		/// </summary>
		/// <param name="entity"></param>
		/// <returns></returns>
		public override bool Add(ContacterEntity entity)
		{
			if (entity.UserType == (int)ContacterType.EnterpriceMainContacter)
			{
				Update("UserType=2", " and UserType=1 and ClientID=@ClientID", new { entity.ClientID });
			}
			if (entity.ContacterID <= 0)
			{
				entity.ContacterID = GetNewID();
			}
			return base.Add(entity);
		}
		/// <summary>
		/// 添加数据。
		/// 1、检查要添加的数据中，联系人类型（UserType）如果为主联系人（企业），则修改数据库中的UserType为第二联系人（企业）。
		/// 2、添加数据。
		/// </summary>
		/// <param name="entity"></param>
		/// <returns></returns>
		public override async Task<bool> AddAsync(ContacterEntity entity)
		{
			if (entity.UserType == (int)ContacterType.EnterpriceMainContacter)
			{
				Update("UserType=2", " and UserType=1 and ClientID=@ClientID", new { entity.ClientID });
			}
			if (entity.ContacterID <= 0)
			{
				entity.ContacterID = GetNewID();
			}
			return await base.AddAsync(entity);
		}
		#endregion

		#region 删除
		/// <summary>
		/// 通过主键批量删除，同时删除U_UserText、ClientLog和RemindItem的数据。
		/// </summary>
		/// <param name="contacterID">多个ID用“,”分隔</param>
		/// <returns></returns>
		public bool DeleteByID(string contacterID)
		{
			return m_repository.DeleteByID(contacterID);
		}
		/// <summary>
		/// 通过主键批量删除，同时删除U_UserText、ClientLog和RemindItem的数据。
		/// </summary>
		/// <param name="contacterID">多个ID用“,”分隔</param>
		/// <returns></returns>
		public async Task<bool> DeleteByIDAsync(string contacterID)
		{
			return await DeleteByIDAsync(contacterID);
		}

		/// <summary>
		/// 根据UserName删除Contacter表数据，同时删除U_UserText、ClientLog和RemindItem的数据。
		/// </summary>
		/// <param name="userName"></param>
		/// <returns></returns>
		public bool DeleteByUserName(string userName)
		{
			return DeleteByUserName(userName);
		}
		/// <summary>
		/// 根据UserName删除Contacter表数据，同时删除U_UserText、ClientLog和RemindItem的数据。
		/// </summary>
		/// <param name="userName"></param>
		/// <returns></returns>
		public async Task<bool> DeleteByUserNameAsync(string userName)
		{
			return await DeleteByUserNameAsync(userName);
		}
		#endregion

		#region 是否存在扩展数据
		/// <summary>
		/// 是否存在扩展数据
		/// </summary>
		/// <param name="id"></param>
		/// <param name="tableName"></param>
		/// <returns></returns>
		public bool IsExistDataOption(int id, string tableName = "U_UserText")
		{
			return m_repository.IsExistDataOption(id,tableName);
		}
		/// <summary>
		/// 是否存在扩展数据
		/// </summary>
		/// <param name="id"></param>
		/// <param name="tableName"></param>
		/// <returns></returns>
		public async Task<bool> IsExistDataOptionAsync(int id, string tableName = "U_UserText")
		{
			return await m_repository.IsExistDataOptionAsync(id, tableName);
		}
		#endregion

		#region 添加扩展数据
		/// <summary>
		/// 添加联系人扩展表数据:从contacterEntity.Fields中获取表列及列值信息
		/// </summary>
		/// <param name="tableName"></param>
		/// <param name="contacterEntity"></param>
		/// <returns></returns>
		public bool AddDataOption(string tableName, ContacterEntity contacterEntity)
		{
			return m_repository.AddDataOption(tableName, contacterEntity);
		}
		/// <summary>
		/// 添加联系人扩展表数据:从contacterEntity.Fields中获取表列及列值信息
		/// </summary>
		/// <param name="tableName"></param>
		/// <param name="contacterEntity"></param>
		/// <returns></returns>
		public async Task<bool> AddDataOptionAsync(string tableName, ContacterEntity contacterEntity)
		{
			return await m_repository.AddDataOptionAsync(tableName, contacterEntity);
		}
		#endregion

		#region 修改扩展数据
		/// <summary>
		/// 修改联系人扩展表数据，不存在则添加:从contacterEntity.Fields中获取表列及列值信息
		/// </summary>
		/// <param name="tableName"></param>
		/// <param name="contacterEntity"></param>
		/// <returns></returns>
		public bool UpdateDataOption(string tableName, ContacterEntity contacterEntity)
		{
			return m_repository.UpdateDataOption(tableName, contacterEntity);
		}
		/// <summary>
		/// 修改联系人扩展表数据，不存在则添加:从contacterEntity.Fields中获取表列及列值信息
		/// </summary>
		/// <param name="tableName"></param>
		/// <param name="contacterEntity"></param>
		/// <returns></returns>
		public async Task<bool> UpdateDataOptionAsync(string tableName, ContacterEntity contacterEntity)
		{
			return await m_repository.UpdateDataOptionAsync(tableName, contacterEntity);
		}
		#endregion

		#region 删除扩展数据
		/// <summary>
		/// 删除扩展数据
		/// </summary>
		/// <param name="tableName"></param>
		/// <param name="id"></param>
		/// <returns></returns>
		public bool DeleteDataOption(string tableName, int id)
		{
			return m_repository.DeleteDataOption(tableName,id);
		}
		/// <summary>
		/// 删除扩展数据
		/// </summary>
		/// <param name="tableName"></param>
		/// <param name="id"></param>
		/// <returns></returns>
		public async Task<bool> DeleteDataOptionAsync(string tableName, int id)
		{
			return await m_repository.DeleteDataOptionAsync(tableName, id);
		}
		#endregion

		#region 得到扩展数据
		/// <summary>
		/// 根据主键得到扩展数据
		/// </summary>
		/// <param name="contacterID"></param>
		/// <param name="writeAndRead"></param>
		/// <returns></returns>
		public DataTable GetDataOption(int contacterID, WriteAndReadEnum writeAndRead = WriteAndReadEnum.None)
		{
			return m_repository.GetDataOption(contacterID, writeAndRead);
		}
		/// <summary>
		/// 根据主键得到扩展数据
		/// </summary>
		/// <param name="contacterID"></param>
		/// <param name="writeAndRead"></param>
		/// <returns></returns>
		public async Task<DataTable> GetDataOptionAsync(int contacterID, WriteAndReadEnum writeAndRead = WriteAndReadEnum.None)
		{
			return await m_repository.GetDataOptionAsync(contacterID, writeAndRead);
		}
		#endregion
	}
}